/*
 * Project: push-center-service-impl
 * 
 * File Created at 2014-10-9
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.service.core.throttle;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import com.greenline.push.core.enums.Channels;
import com.greenline.push.core.pushcenter.ThrottleManager;
import com.greenline.push.dal.pushcenter.dataobject.ThrottleDO;

/**
 * throttle 工厂 各个渠道的阀值保存在内存中.并且提供修改的方法. 满足配置端配置完成以后刷新配置
 * 
 * @author weirui.shenwr
 * @date 2014-10-9
 * @version V1.0
 */
public class ThrottleMangerCache {

    private static ConcurrentHashMap<Channels, ChannelThrottle> channelThrottleMap = new ConcurrentHashMap<Channels, ChannelThrottle>();
    @Resource
    private ThrottleManager throttleManager;

    /**
     * 
     * @param channel
     * @return
     */
    public ChannelThrottle getChannelThrottle(Channels channel) {
        ChannelThrottle throttle = channelThrottleMap.get(channel);
        if (null == throttle) {
            reloadThrootleConfigByChannel(channel);
            throttle = channelThrottleMap.get(channel);
        }
        return channelThrottleMap.get(channel);
    }

    /**
     * 
     * @param channel
     * @return
     */
    public Integer getChannelThreasHold(Channels channel) {
        ChannelThrottle throttle = channelThrottleMap.get(channel);
        if (null == throttle) {
            reloadThrootleConfigByChannel(channel);
            throttle = channelThrottleMap.get(channel);
        }
        return channelThrottleMap.get(channel).getThreshold();
    }

    /**
     * 启动时加载
     */
    public void initialThrottleConfig() {
        List<ThrottleDO> throttleList = throttleManager.listByExample(null);
        System.out.println("ThrottleMangerCache initial start=====");
        for (ThrottleDO tmp : throttleList) {
            Channels channel = Channels.getChannelbyType(String.valueOf(tmp.getType()));
            ChannelThrottle tho = new ChannelThrottle();
            tho.setChannel(channel);
            tho.setThreshold(tmp.getValue());
            channelThrottleMap.put(channel, tho);
        }
        System.out.println("ThrottleMangerCache initial end=====");
    }

    /**
     * 加载整个
     */
    public boolean reloadAllThrottleConfig() {
        List<ThrottleDO> throttleList = throttleManager.listByExample(null);
        if (null != throttleList) {
            for (ThrottleDO tmp : throttleList) {
                Channels channel = Channels.getChannelbyType(String.valueOf(tmp.getType()));
                ChannelThrottle tho = new ChannelThrottle();
                tho.setChannel(channel);
                tho.setThreshold(tmp.getValue());
                channelThrottleMap.put(channel, tho);
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean reloadThrootleConfigByChannel(Channels channel) {
        String type = channel.getType();
        ThrottleDO th = new ThrottleDO();
        th.setType(Integer.parseInt(type));
        List<ThrottleDO> throttleList = throttleManager.listByExample(th);
        if (null != throttleList) {
            ThrottleDO tmp = throttleList.get(0);
            ChannelThrottle chan = new ChannelThrottle();
            chan.setThreshold(tmp.getValue());
            chan.setChannel(channel);
            channelThrottleMap.put(channel, null);
            return true;
        } else {
            return false;
        }
    }

}
